/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function calTime(){
    var currentETime = new Date();
    var eTime = currentETime.getTime();
    $('#responseTime').html(eTime - sTime);
}
var sTime;
function startTime(){
    var currentSTime = new Date();
    sTime = currentSTime.getTime();
    $('#responseTime').html("<img src=\"./images/loader.gif\" />");
}

 


function loadMailTable(){
    
    startTime();
    $("#showTable").html("<img src=\"./images/loader.gif\" />");
    $("#showTable").load("MailListServlet?action=maillisttable").hide().fadeIn('slow');
    calTime();
  

}
function loadMailGroup(){

    startTime();
    $("#mailgroup").html("<img src=\"./images/loader.gif\" />");
    $("#mailgroup").load("MailListServlet?action=mailgrouplist").hide().fadeIn('slow');
    calTime();


}

jQuery(document).ready(function(){
    jQuery("#formID").validationEngine('attach', {
        onValidationComplete: function(form, status){
            if(status){

                var mail1=$("#mail").val();
                var mailgp=$("#mailgroup").val();

                $.post(
                    "MailListServlet",
                    {
                        action:"checkMail",
                        mail:mail1
                    },
                    function(data){
                        if (data.success){
                            alert("Email already in mail list ");


                        }else{
                            $.post(
                                "MailListServlet",
                                {
                                    action:"addMail",
                                    mail:mail1,
                                    mailgroup:mailgp
                                },
                                function(data){
                                    if (data.success){
                                        alert("Success to add email ");
                                        loadMailTable();
                                        $("#mail").val("");

                                    }else{
                                        alert("Error can't edit data ");
                                    }
                                },

                                "json"

                                );
                        }
                    },

                    "json"

                    );

                
                         
              
            }
        }
    });
});

function deleteMail(mail1){
    if(confirm('Confirm to delete mail : '+mail1+'')==true)
    {
        $.post(
            "MailListServlet",
            {
                action: "delMail",
                mail: mail1

            },
            function(data){
                if (data.success){
                    alert("success to delete email ");
                    loadMailTable();
                }
                else
                    alert("Error can't delete email ");
            },
            "json"

            );
    }
}


